﻿using System.Collections.Generic;
using TrueSync;
using UnityEngine;

namespace HEFramework
{
    public static class SkillCastComponentSystem
    {
        [EntitySystem]
        public class SkillCastComponentAwakeSystem : AwakeSystem<SkillCastComponent>
        {
            protected override void Awake(SkillCastComponent _self)
            {
            }
        }


        [EntitySystem]
        public class SkillCastComponentUpdateSystem : UpdateSystem<SkillCastComponent>
        {
            protected override void Update(SkillCastComponent _self)
            {
                var dc = _self.GetSceneChild<DataTableComponent>();
                var sc = _self.GetParent<SkillComponent>();
                if (dc == null || sc == null)
                {
                    return;
                }

                var sec = sc.GetChild<SkillEffectComponent>();
                if (sec == null)
                {
                    return;
                }

                var sect = sec.GetChild<SkillEffectTargetComponent>();
                if (sect == null)
                {
                    return;
                }

                //非吟唱中
                if (_self.CastTime <= 0)
                {
                    return;
                }

                if (sect.IsSingle(_self.CastID, true))
                {
                    //对象消失了 结束引导
                    if (_self.CastEntity.Entity == null)
                    {
                        _self.Refresh();
                        return;
                    }

                    //时刻更新 对象位置
                    var tc = _self.CastEntity.Entity.GetChild<TransformComponent>();
                    if (tc != null)
                    {
                        _self.CastPos = tc.P;
                    }
                }

                FP deltaTime = Time.deltaTime;

                //吟唱中
                _self.CastTime -= deltaTime;
                if (_self.CastTime <= 0)
                {
                    //吟唱结束
                    _self.Completed();
                }
            }
        }


        /// <summary>
        ///  吟唱技能
        /// </summary>
        /// <param name="_self"></param>
        /// <param name="_id"></param>
        /// <param name="_receiver"></param>
        /// <param name="_pos"></param>
        public static void Cast(this SkillCastComponent _self, int _id, Entity _receiver, TSVector _pos)
        {
            var dc = _self.GetSceneChild<DataTableComponent>();
            if (dc == null)
            {
                return;
            }

            _self.Refresh();
            _self.CastID = _id;
            _self.CastEntity = _receiver;
            _self.CastPos = _pos;
            var sData = dc.LoadDataTable<DRSkill>().GetData(_id);
            //无需吟唱
            if (sData.ACasting <= 0)
            {
                _self.Completed();
                return;
            }

            _self.CastTime = Formula.TM(sData.ACasting);
        }

        /// <summary>
        /// 完成
        /// </summary>
        /// <param name="_self"></param>
        private static void Completed(this SkillCastComponent _self)
        {
            var sec = _self.GetParentChild<SkillExecutionComponent>();
            sec.Execute(_self.CastID, _self.CastEntity, _self.CastPos, true);
            _self.Refresh();
        }

        private static void Refresh(this SkillCastComponent _self)
        {
            _self.CastID = 0;
            _self.CastEntity = null;
            _self.CastPos = default;
            _self.CastTime = 0;
        }
    }
}